/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cnaps2_parse.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/14 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frct_comm.h"

#include "../../../src/module/ibmmq/config.h"
#include "../test_ibmmq_comm.h"
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <ibmmq/cnaps2.h>
#include "../../../src/exec/framework/frreport_session_mng.c"

static Frmemstat_mng g_st_memstat_mng={0,FR_MEMSTAT_COUNT_MAX};
static pthread_rwlock_t  g_memstat_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static Frmem_env g_st_mem_env = {&g_st_memstat_mng, &g_memstat_rwlock};

FR_STATIC Frexp_ref g_st_expRef={NULL, NULL};
FR_STATIC Frexp_ref g_st_exp2Ref={NULL, NULL};
FR_STATIC Frservice_ref g_st_serviceRef={NULL, NULL};
FR_STATIC Frreport_session_ref g_st_report_session_Ref={NULL, NULL, NULL};
FR_STATIC Report_module g_st_report_moduleRef = {NULL, NULL, NULL, NULL, NULL};

ApmContext g_st_apm_context = {&g_st_expRef, &g_st_exp2Ref, &g_st_serviceRef, &g_st_report_session_Ref, &g_st_report_moduleRef, &g_st_mem_env};
void *g_p_apm_context = NULL;

static int __suite_init(void)
{
    cnaps2_mesg_type_quicksort(0, -1); // 对arr_mesg_type_maps进行快速排序
    cnaps2_mesg_resp_quicksort(0, -1); // 对arr_mesg_resp进行快速排序
    frreport_session_mng_init(0);
    frreport_session_mng_setRef(g_st_apm_context.p_st_report_session_ref);
    //frreport_session_set(&g_st_apm_context, MODULE_L7PROTO, cnaps2_session_free);
    g_p_apm_context = &g_st_apm_context;
    return 0;
}

static int __suite_cleanup(void)
{
    frreport_session_mng_destory();
    return 0;
}

/**
 * 正常测试
 * 请求
 */
static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_ibmmq_create_ct(&p_st_conn, TEST_IBMMQ_SRC_IP, TEST_IBMMQ_DST_IP, TEST_IBMMQ_SRC_PORT, TEST_IBMMQ_DST_PORT), FR_SUCC);

    Frreport_body_data *p_st_body_req = NULL;
    Frreport_data *p_st_report_data = NULL;
    //Frreport_body_data *p_st_data_req_tmp = NULL;

    p_st_body_req = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body_req);

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body_req, APM_REPORT_KEY_SESSION_ID, TEST_IBMMQ_SESSION_ID), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body_req, APM_REPORT_KEY_TRANS_TITLE, "PUT MSGCNAPS_1"), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_connInfo(p_st_body_req, p_st_conn), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_ulong(p_st_body_req, APM_REPORT_KEY_REQ_ID, frct_seq_increase(p_st_conn)), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_int(p_st_body_req, APM_REPORT_KEY_REQ_ISRESPREQUIRED, FR_YES), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_timeval(p_st_body_req, APM_REPORT_KEY_REQ_START_TIMESTAMP, &st_timestamp), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_timeval(p_st_body_req, APM_REPORT_KEY_REQ_END_TIMESTAMP, &st_timestamp), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_long(p_st_body_req, APM_REPORT_KEY_REQ_SEND_TIME_US, 0L), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_ulong(p_st_body_req, APM_REPORT_KEY_REQ_DATA_BYTES, 12UL), FR_SUCC);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body_req, "request.mq.message.text", "{H:02313513080408  HVPS316331000018  HVPS20151120152414XMLhvps.111.001.01     2015112020005682    2015112020077519    3D         }\r\n{S:AWO9+rFlK7VevSvpGKQMhk/AGKzEeqA4722IYXzEWUNyvuS4Ss3acAB9qK4x0iHE\nMeFMJYr8ciqYFFnnB0WKdcVo0UG93lMn7IHHJffSG8hGUmsdbvwkZBWEu2FDh/Xs\nzRo5gNb+CJevSn2CayV7bNx3arm0ZnB2GoehL14Z5Qs=\n}\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02\">\n\t<FIToFICstmrCdtTrf>\n\t\t<GrpHdr>\n\t\t\t<MsgId>2015112020005682</MsgId>\n\t\t\t<CreDtTm>2015-11-20T15:24:13</CreDtTm>\n\t\t\t<NbOfTxs>1</NbOfTxs>\n\t\t\t<SttlmInf>\n\t\t\t\t<SttlmMtd>CLRG</SttlmMtd>\n\t\t\t</SttlmInf>\n\t\t</GrpHdr>\n\t\t<CdtTrfTxInf>\n\t\t\t<PmtId>\n\t\t\t\t<EndToEndId>000000000001</EndToEndId>\n\t\t\t\t<TxId>2015112020005682</TxId>\n\t\t\t</PmtId>\n\t\t\t<PmtTpInf>\n\t\t\t\t<CtgyPurp>\n\t\t\t\t\t<Prtry>A113</Prtry>\n\t\t\t\t</CtgyPurp>\n\t\t\t</PmtTpInf>\n\t\t\t<IntrBkSttlmAmt Ccy=\"CNY\">201511.20</IntrBkSttlmAmt>\n\t\t\t<SttlmPrty>NORM</SttlmPrty>\n\t\t\t<ChrgBr>DEBT</ChrgBr>\n\t\t\t<InstgAgt>\n\t\t\t\t<FinInstnId>\n\t\t\t\t\t<ClrSysMmbId>\n\t\t\t\t\t\t<MmbId>103100000026</MmbId>\n\t\t\t\t\t</ClrSysMmbId>\n\t\t\t\t</FinInstnId>\n\t\t\t\t<BrnchId>\n\t\t\t\t\t<Id>103100000026</Id>\n\t\t\t\t</BrnchId>\n\t\t\t</InstgAgt>\n\t\t\t<InstdAgt>\n\t\t\t\t<FinInstnId>\n\t\t\t\t\t<ClrSysMmbId>\n\t\t\t\t\t\t<MmbId>316331000018</MmbId>\n\t\t\t\t\t</ClrSysMmbId>\n\t\t\t\t</FinInstnId>\n\t\t\t\t<BrnchId>\n\t\t\t\t\t<Id>316331000106</Id>\n\t\t\t\t</BrnchId>\n\t\t\t</InstdAgt>\n\t\t\t<Dbtr>\n\t\t\t\t<Nm>建行</Nm>\n\t\t\t\t<PstlAdr>\n\t\t\t\t\t<AdrLine>建行</AdrLine>\n\t\t\t\t</PstlAdr>\n\t\t\t</Dbtr>\n\t\t\t<DbtrAcct>\n\t\t\t\t<Id>\n\t\t\t\t\t<Othr>\n\t\t\t\t\t\t<Id>11001071600053006079</Id>\n\t\t\t\t\t</Othr>\n\t\t\t\t</Id>\n\t\t\t</DbtrAcct>\n\t\t\t<DbtrAgt>\n\t\t\t\t<FinInstnId>\n\t\t\t\t\t\t<ClrSysMmbId>\n\t\t\t\t\t\t<MmbId>103100000026</MmbId>\n\t\t\t\t\t</ClrSysMmbId>\n\t\t\t\t\t<Nm>中国农业银行资金清算中心</Nm>\n</FinInstnId>\n\t\t\t</DbtrAgt>\n\t\t\t<CdtrAgt>\n\t\t\t\t<FinInstnId>\n\t\t\t\t\t\t<ClrSysMmbId>\n\t\t\t\t\t\t<MmbId>316331000106</MmbId>\n\t\t\t\t\t</ClrSysMmbId>\n\t\t\t\t\t<Nm>浙商银行杭州分行营业部</Nm>\n</FinInstnId>\n\t\t\t</CdtrAgt>\n\t\t\t<Cdtr>\n\t\t\t\t<Nm>test</Nm>\n\t\t\t</Cdtr>\n\t\t\t<CdtrAcct>\n\t\t\t\t<Id>\n\t\t\t\t\t<Othr>\n\t\t\t\t\t\t<Id>3310010010110100296128</Id>\n\t\t\t\t\t</Othr>\n\t\t\t\t</Id>\n\t\t\t</CdtrAcct>\n\t\t\t<Purp>\n\t\t\t\t<Prtry>02112</Prtry>\n\t\t\t</Purp>\n\t\t\t<RmtInf>\n\t\t\t\t<Ustrd>/H01/备注备注</Ustrd>\n\t\t\t\t<Ustrd>/H02/备注备注1</Ustrd>\n\t\t\t\t<Ustrd>/H03/备注附言</Ustrd>\n\t\t\t\t<Ustrd>/H04/备注附言1</Ustrd>\n\t\t\t\t<Ustrd>/C00/2015-11-20</Ustrd>\n\t\t\t\t<Ustrd>/C14/2015-11-20</Ustrd>\n\t\t\t\t<Ustrd>/F56/OUR</Ustrd>\n\t\t\t\t<Ustrd>/D63/CNY0.00</Ustrd>\n\t\t\t\t<Ustrd>/D64/CNY0.00</Ustrd>\n\t\t\t\t<Ustrd>/H19/跨境业务附言跨境业务附言</Ustrd>\n\t\t\t\t<Ustrd>/B29/3310010010120192197208</Ustrd>\n\t\t\t</RmtInf>\n\t\t</CdtTrfTxInf>\n\t</FIToFICstmrCdtTrf>\n</Document>\n"), FR_SUCC);

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    CU_ASSERT_EQUAL(frreport_data_add_body(p_st_report_data, p_st_body_req), FR_SUCC);
    //p_st_data_req_tmp = p_st_body_req;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    int i_cnaps2_ret = FR_SUCC;
    i_cnaps2_ret = cnaps2_parse(p_st_conn, p_st_report_data, &st_hash_report, FR_YES);
    CU_ASSERT_EQUAL(i_cnaps2_ret, FR_SUCC);

    if (frreport_data_hash(&st_hash_report, p_st_report_data) == FR_FAIL)
        frreport_data_free(&p_st_report_data);
    p_st_report_data = NULL;

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data2 = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);
    CU_ASSERT_PTR_NOT_NULL(p_list_report);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data2 = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data2);

    p_st_field = FrReportOpt_get_field(p_st_report_data2, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "PUT MSGCNAPS_1");

    p_st_field = FrReportOpt_get_field(p_st_report_data2, "transaction.type_code", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "hvps.111.001.01");

    p_st_field = FrReportOpt_get_field(p_st_report_data2, "transaction.type_name", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "客户发起汇兑业务");

    frhash2_free(&st_hash_report, list_report_node_free);

    frreport_data_free(&p_st_report_data);

    test_ibmmq_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 响应
 */
#if 0
static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_ibmmq_create_ct(&p_st_conn, TEST_IBMMQ_SRC_IP, TEST_IBMMQ_DST_IP, TEST_IBMMQ_SRC_PORT, TEST_IBMMQ_DST_PORT), FR_SUCC);

    Frreport_body_data *p_st_body_req = NULL;
    Frreport_data *p_st_report_data = NULL;
    Frreport_body_data *p_st_data_req_tmp = NULL;

    p_st_body_req = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body_req);

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body_req, APM_REPORT_KEY_SESSION_ID, TEST_IBMMQ_SESSION_ID), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body_req, APM_REPORT_KEY_TRANS_TITLE, "PUT MSGCNAPS_1"), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_connInfo(p_st_body_req, p_st_conn), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_ulong(p_st_body_req, APM_REPORT_KEY_REQ_ID, frct_seq_increase(p_st_conn)), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_int(p_st_body_req, APM_REPORT_KEY_REQ_ISRESPREQUIRED, FR_YES), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_timeval(p_st_body_req, APM_REPORT_KEY_REQ_START_TIMESTAMP, &st_timestamp), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_timeval(p_st_body_req, APM_REPORT_KEY_REQ_END_TIMESTAMP, &st_timestamp), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_long(p_st_body_req, APM_REPORT_KEY_REQ_SEND_TIME_US, 0L), FR_SUCC);
    CU_ASSERT_EQUAL(frreport_body_l7_add_ulong(p_st_body_req, APM_REPORT_KEY_REQ_DATA_BYTES, 12UL), FR_SUCC);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body_req, "request.mq.message.text", "{H:02316331000018  HVPS0000          HVPS20151120153812XMLccms.990.001.02     316331000018003346202015112020005682    3U         }\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><Document xmlns=\"urn:cnaps:std:ccms:2010:tech:xsd:ccms.990.001.02\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><ComConf><ConfInf><OrigSndr>313513080408</OrigSndr><OrigSndDt>20151120</OrigSndDt><MT>hvps.111.001.01</MT><MsgId>2015112020005682</MsgId><MsgRefId>2015112020077519</MsgRefId><MsgPrcCd>PM1I0000</MsgPrcCd></ConfInf></ComConf></Document>"), FR_SUCC);

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    CU_ASSERT_EQUAL(frreport_data_add_body(p_st_report_data, p_st_body_req), FR_SUCC);
    p_st_data_req_tmp = p_st_body_req;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    int i_cnaps2_ret = FR_SUCC;
    i_cnaps2_ret = cnaps2_parse(p_st_conn, p_st_report_data, p_st_data_req_tmp, &st_hash_report);
    CU_ASSERT_EQUAL(i_cnaps2_ret, CNAPS2_PARSE_DROP_CURRENT);

    if (i_cnaps2_ret == CNAPS2_PARSE_DROP_CURRENT)
        frreport_data_free(&p_st_report_data);
    else if (frreport_data_hash(&st_hash_report, p_st_report_data) == FR_FAIL)
        frreport_data_free(&p_st_report_data);
    p_st_report_data = NULL;

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data2 = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);
    CU_ASSERT_PTR_NOT_NULL(p_list_report);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data2 = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data2);

    p_st_field = FrReportOpt_get_field(p_st_report_data2, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "PUT MSGCNAPS_1");

    p_st_field = FrReportOpt_get_field(p_st_report_data2, "transaction.type_code", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "hvps.111.001.01");

    p_st_field = FrReportOpt_get_field(p_st_report_data2, "transaction.type_name", 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "客户发起汇兑业务");

    p_st_field = FrReportOpt_get_field(p_st_report_data2, APM_REPORT_KEY_RESP_CODE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0LL);

    p_st_field = FrReportOpt_get_field(p_st_report_data2, APM_REPORT_KEY_RESP_CODE_STRING, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "PM1I0000");

    p_st_field = FrReportOpt_get_field(p_st_report_data2, APM_REPORT_KEY_RESP_ERR_MSG, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "PM1I0000:支付报文传输平台-NPC开放系统-处理成功");

    frhash2_free(&st_hash_report, list_report_node_free);

    frreport_data_free(&p_st_report_data);

    test_ibmmq_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}
#endif

int add_suite_cnaps2_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cnaps2_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
